#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define PATH "/home/shy"
#define ID 0x6666
#define SIZE 4096



int main()
{
	key_t key = ftok(PATH, ID); //获取与server进程相同的key值
	if (key < 0){
		perror("ftok");
		return 1;
	}
	int shm = shmget(key, SIZE, IPC_CREAT); //获取server进程创建的共享内存的用户层id
	if (shm < 0){
		perror("shmget");
		return 2;
	}

	printf("key: %x\n", key); //打印key值
	printf("shm: %d\n", shm); //打印共享内存用户层id

	char* mem = (char*)shmat(shm, NULL, 0); //关联共享内存
  char buff[128];
	while (1)
  {
    ssize_t s = read(0 , buff , sizeof(buff)-1);
    buff[s-1] = 0;
    memcpy(mem,buff,sizeof(buff));
    buff[0] = 0;
    sleep(1);
    mem[0] = 0;
  }
	shmdt(mem); //共享内存去关联
	return 0;
}

